package com.bignerdranch.android.expresshelper.Activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.bignerdranch.android.expresshelper.Adapter.ResultAdapter;
import com.bignerdranch.android.expresshelper.Bean.ExpressBean;
import com.bignerdranch.android.expresshelper.Dao.ExpressDao;
import com.bignerdranch.android.expresshelper.R;
import com.bignerdranch.android.expresshelper.Utils.CheckNet;
import com.bignerdranch.android.expresshelper.Utils.RequestUtil;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class ResultActivity extends AppCompatActivity  {

    private TextView mTopTextview;
    private ListView mListView;
    private ResultAdapter mAdapter;
    private String url = "http://route.showapi.com/64-19?showapi_appid=20076&showapi_sign=b697ebeae51b4525b7ea856bade2d325";
    private String nu = "";
    private String com = "";
    private String urlExtra = "";
    private ImageView companyIcon;
    private TextView companyName;
    private TextView companyNum;
    public static String status;
    private ExpressDao mDao;
    private LinearLayout noresult;
    private LinearLayout numwrong;
    private LinearLayout progress;
    private ImageButton mLeft;
    private Button refresh;
    private LinearLayout wifi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        initView();
        nu = getIntent().getStringExtra("num");
        com = getIntent().getStringExtra("name");
        urlExtra = url + "&nu=" + nu + "&com=" + com;
        intiTitle();

        if (CheckNet.getNet(this)) {


            initResult();
        } else {
            wifi.setVisibility(View.VISIBLE);
            noresult.setVisibility(View.INVISIBLE);
            mListView.setVisibility(View.INVISIBLE);
            numwrong.setVisibility(View.INVISIBLE);
            progress.setVisibility(View.INVISIBLE);
        }


    }

    private void initResult() {

        RequestUtil.executeJSONObject(this, urlExtra, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    Log.e("------>>", o.getString("showapi_res_code"));
                    if (o.getString("showapi_res_code").trim().equals("-2")||
                            o.getJSONObject("showapi_res_body").getString("flag").equals("false")) {
                        noresult.setVisibility(View.VISIBLE);
                        mListView.setVisibility(View.INVISIBLE);
                        numwrong.setVisibility(View.INVISIBLE);
                        progress.setVisibility(View.INVISIBLE);
                        wifi.setVisibility(View.INVISIBLE);
                    }

                    if (o.getJSONObject("showapi_res_body").getString("status") == "1") {
                        noresult.setVisibility(View.INVISIBLE);
                        mListView.setVisibility(View.INVISIBLE);
                        numwrong.setVisibility(View.VISIBLE);
                        progress.setVisibility(View.INVISIBLE);
                        wifi.setVisibility(View.INVISIBLE);
                        Log.e("---numwrong--->>", o.getString("showapi_res_code"));
                    } else {
                        if (o.getJSONObject("showapi_res_body").getJSONArray("data").length() > 0) {
                            noresult.setVisibility(View.INVISIBLE);
                            mListView.setVisibility(View.VISIBLE);
                            numwrong.setVisibility(View.INVISIBLE);
                            progress.setVisibility(View.INVISIBLE);
                            wifi.setVisibility(View.INVISIBLE);
                            mAdapter = new ResultAdapter(o.getJSONObject("showapi_res_body").getJSONArray("data")
                                    , getApplicationContext());
                            mListView.setAdapter(mAdapter);
//                            Log.e("----mListView-->>", o.getString("showapi_res_code"));
                        } else {
                            noresult.setVisibility(View.VISIBLE);
                            mListView.setVisibility(View.INVISIBLE);
                            numwrong.setVisibility(View.INVISIBLE);
                            progress.setVisibility(View.INVISIBLE);
                            wifi.setVisibility(View.INVISIBLE);
                            Log.e("----noresult-->>", o.getString("showapi_res_code"));
                        }


                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.e("----JSONException-->>", e.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

                Log.e("---volleyError->>",volleyError.getMessage());
                wifi.setVisibility(View.VISIBLE);
                noresult.setVisibility(View.INVISIBLE);
                mListView.setVisibility(View.INVISIBLE);
                numwrong.setVisibility(View.INVISIBLE);
                progress.setVisibility(View.INVISIBLE);
            }
        });
    }


    private void intiTitle() {
        companyIcon = (ImageView) this.findViewById(R.id.company_icon);
        companyName = (TextView) this.findViewById(R.id.company_name);
        companyNum = (TextView) this.findViewById(R.id.company_num);
        mDao = new ExpressDao(getApplicationContext());
        ExpressBean bean = mDao.query(nu);
        File file = new File(getFilesDir(), bean.getExpressIcon() + ".jpg");
//        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()+".jpg");
//        Log.e("---bitmap>>", file.getAbsolutePath() + ".jpg");
        Picasso.with(getApplicationContext())
                .load(file)
                .fit()
                .into(companyIcon);
//        companyIcon.setImageBitmap(bitmap);
        String mailNo = bean.getExpressNum();
        String expTextName = bean.getExpressName();
        companyName.setText(expTextName);
        companyNum.setText(mailNo);
        RequestQueue queue = RequestUtil.getRequestQueue(getApplicationContext());

        JsonRequest request = new JsonObjectRequest(urlExtra, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                try {
//                    String mailNo = jsonObject.getJSONObject("showapi_res_body").getString("mailNo");
                    status = jsonObject.getJSONObject("showapi_res_body").getString("status");
//                    String expTextName = jsonObject.getJSONObject("showapi_res_body").getString("expTextName");
//                    companyName.setText(expTextName);
//                    companyNum.setText(mailNo);
                    mDao.updateStatus(nu, status);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

                //  Log.e("---->>>", volleyError.getMessage());
            }
        });
        queue.add(request);
    }

    private void initView() {
        refresh = (Button) findViewById(R.id.refresh_result);
        wifi = (LinearLayout) findViewById(R.id.wifi);
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CheckNet.getNet(getApplicationContext())) {
                    wifi.setVisibility(View.INVISIBLE);
                    noresult.setVisibility(View.INVISIBLE);
                    mListView.setVisibility(View.INVISIBLE);
                    numwrong.setVisibility(View.INVISIBLE);
                    progress.setVisibility(View.VISIBLE);
                    intiTitle();
                    initResult();
                } else {
                    wifi.setVisibility(View.VISIBLE);
                    noresult.setVisibility(View.INVISIBLE);
                    mListView.setVisibility(View.INVISIBLE);
                    numwrong.setVisibility(View.INVISIBLE);
                    progress.setVisibility(View.INVISIBLE);
                }
            }
        });

        mLeft = (ImageButton) this.findViewById(R.id.top_img_left);
        mLeft.setImageResource(R.drawable.back);
        mLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTopTextview = (TextView) findViewById(R.id.top_text);

        mTopTextview.setText(R.string.result);
        mListView = (ListView) findViewById(R.id.list);
        noresult = (LinearLayout) findViewById(R.id.noresult);
        numwrong = (LinearLayout) findViewById(R.id.numwrong);
        progress = (LinearLayout) findViewById(R.id.progress);





    }

   /* @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.refresh_result:
                if (CheckNet.getNet(this)) {
                    initResult();
                } else {
                    wifi.setVisibility(View.VISIBLE);
                    noresult.setVisibility(View.INVISIBLE);
                    mListView.setVisibility(View.INVISIBLE);
                    numwrong.setVisibility(View.INVISIBLE);
                    progress.setVisibility(View.INVISIBLE);
                }
                break;
        }
    }*/
}
